package com.command.command;

/**
 * 遥控器类
 * 里面有各种开关按钮
 *
 */
public class RemotController {
    //存放开命令 相当于一排开按钮
    private Command[] onCommands ;
    //存放关命令 相当于一排关按钮
    private Command[] offCommands ;
    //执行撤销的命令 记住上一次操作的命令对象是谁
    private Command undo;

    //构造
    //初始化遥控器上按钮的命令，刚开始的时候，所有按钮按钮都是命令
    public RemotController() {
        //假设开 、 关 按钮各有5个
        onCommands = new Command[5];
        offCommands = new Command[5];
        for (int i = 0; i < 5; i++) {
            onCommands[i] = new NoCommand();
            offCommands[i] = new NoCommand();
        }
    }

    /**
     * 为某个开按钮设置功能
     * @param buttonIndex 按钮的行索引 ，每一行有两个按钮
     * @param on
     * @param off
     */
    public void setCommandForButton(int buttonIndex , Command on , Command off){
        onCommands[buttonIndex] = on;
        offCommands[buttonIndex] = off;
    }

    /**
     * 按下了某个开按钮
     * @param buttonIndex
     */
    public void onButtonWasPushed(int buttonIndex){
        onCommands[buttonIndex].execute();
        //记录当前点的是那个按钮
        undo = onCommands[buttonIndex];
    }

    /**
     * 按下了某个关按钮
     * @param buttonIndex
     */
    public void offButtonWasPushed(int buttonIndex){
        offCommands[buttonIndex].execute();
        //记录当前点的是那个按钮
        undo = offCommands[buttonIndex];
    }

    /**
     * 点击了撤销按钮
     */
    public void back(){
        undo.undo();
    }
}
